<?php

namespace app\api\model;

use app\api\model\Common;
use think\Validate;

class LiveSpeech extends Common
{
    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如微信模块用weixin作为数据表前缀
     */
    protected $name = 'meeting_live_speech';
    //关联liveDetail模型
    public function liveDetail()
    {
        return $this->hasOne('LiveDetail','lid');
    }
    /**
     * 获取列表及列表总数量
     * @return    [array]                       
     */
    public function getDataListAndCount($param, $limit, $page, $time, $match)
    {
        try {
            $list = $this->alias('l')
                ->join('equipment e', 'l.did = e.equipmentid', 'LEFT')
                ->where($param)
                ->where('start_time', 'between', $time)
                ->where('l.filename|e.title|l.did', 'like', '%' . $match . '%')
                ->limit($limit)
                ->page($page)
                ->order('id desc')
                ->select();
            $count = $this->alias('l')
            ->join('equipment e', 'l.did = e.equipmentid', 'LEFT')
            ->where($param)
            ->where('start_time', 'between', $time)
            ->where('l.filename|e.title', 'like', '%' . $match . '%')
            ->count();
            $result = [
                'list' => $list,
                'count' => $count,
            ];
            return $result;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }


    /**
     * [getDataById 根据主键获取详情]
     * @linchuangbin
     * @DateTime  2017-02-10T21:16:34+0800
     * @param     string                   $id [主键]
     * @return    [array]                       
     */
    public function getDataById($id = '')
    {
        $data = $this->get($id);
        if (!$data) {
            throw new \Exception('暂无此数据');
        }
        return $data;
    }


    /**
     * [createData 新建]
     * @linchuangbin
     * @DateTime  2017-02-10T21:19:06+0800
     * @param     array                    $param [description]
     * @return    [array]                         [description]
     */
    public function createData($param)
    {

        //验证
        $validate = validate($this->name);
        if (!$validate->check($param)) {
            $this->error = $validate->getError();
            return false;
        }
        try {
            $this->data($param)->allowField(true)->save();
            return true;
        } catch (\Exception $e) {
            $this->error = '添加失败';
            return false;
        }
    }

        /**
     * [updateDataById 编辑]
     * @linchuangbin
     * @DateTime  2017-02-10T21:24:49+0800
     * @param     [type]                   $param [description]
     * @param     [type]                   $id    [description]
     * @return    [type]                          [description]
     */
    public function updateDataById($param, $id)
    {
        $checkData = $this->get($id);
        if (!$checkData) {
            $this->error = '暂无此数据';
            return false;
        }
        
        // // 验证
        // $validate = new Validate($this->rules);
        // if (!$validate->check($param)) {
        //     $this->error = $validate->getError();
        //     return false;
        // }
        
        try {
            $this->allowField(true)->save($param, [$this->getPk() => $id]);
            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 删除文件
     */
    public function deletDatas($ids = [])
    {
        try {
            $this->where(['is_delete' => 0])->where($this->getPk(), 'in', $ids)->update(['is_delete' => 1]);
            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
}
